package com.zcz.umengpush.service;

import com.zcz.umengpush.mapper.PushMsgMapper;
import com.zcz.umengpush.pojo.PushMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王卫帅
 */
@Service
public class PushMsgService {
    Logger logger = LoggerFactory.getLogger(getClass());
    //ios在成长家长版
    @Value("${umeng4j.zczjzappkeyIos}")
    private String zczjzappkeyIos;
    @Value("${umeng4j.zczjzappMasterSecretIos}")
    private String zczjzappMasterSecretIos;
    //Android在成长家长版
    @Value("${umeng4j.zczjzappkeyAndroid}")
    private String zczjzappkeyAndroid;
    @Value("${umeng4j.zczjzappMasterSecretAndroid}")
    private String zczjzappMasterSecretAndroid;
    //ios在成长教师版
    @Value("${umeng4j.zczjsappkeyIos}")
    private String zczjsappkeyIos;
    @Value("${umeng4j.zczjsappMasterSecretIos}")
    private String zczjsappMasterSecretIos;
    //Android在成长教师版
    @Value("${umeng4j.zczjsappkeyAndroid}")
    private String zczjsappkeyAndroid;
    @Value("${umeng4j.zczjsappMasterSecretAndroid}")
    private String zczjsappMasterSecretAndroid;
    @Autowired
    private PushMsgMapper pushMsgMapper;
    /**
     *
     *获取推送的信息列表(不能超过495条数据,只推送近两个小时未推送的消息;当推送出现错误，允许重试2次，累计推送3次)
     * 每20秒读取一次
     * @return
     */
    public List<PushMsg> selecInfoList (){
        logger.info("正在推送----其他----消息");
        List<PushMsg> pushMsgs =null;

        Integer iOnePageNum = 495;
        Map<String ,Object>map = new HashMap<>();
        map.put("iOnePageNum",iOnePageNum);
        map.put("v_result",null);

        pushMsgMapper.selectInfoList(map);

        pushMsgs = (List<PushMsg>) map.get("v_result");

        //pushMsgs = pushMsgMapper.selectInfoList();
        if (pushMsgs != null){
            for (PushMsg pushMsg : pushMsgs) {
                Integer deviceType = pushMsg.getDeviceType();
                Integer userType = pushMsg.getUserType();
                if (deviceType != 1){
                    System.err.println("其他消息安卓");
                    Demo demo =null;
                    if(userType==0){
                        demo = new Demo(zczjzappkeyAndroid, zczjzappMasterSecretAndroid);
                        try {
                            //安卓推送 家长版
                            demo.sendAndroidUnicast(pushMsg,pushMsg.getUserType());
                        } catch (Exception e) {
                            e.printStackTrace();
                            logger.error("推送失败家长端"+pushMsg.getTokenId());
                        }
                    } else {
                        demo = new Demo(zczjsappkeyAndroid, zczjsappMasterSecretAndroid);
                        try {
                            //安卓推送 教师版
                            demo.sendAndroidUnicast(pushMsg,pushMsg.getUserType());
                        } catch (Exception e) {
                            e.printStackTrace();
                            logger.error("推送失败教师版"+pushMsg.getTokenId());
                        }
                    }

                }else {
                //if(deviceType==3){
                    Demo demo =null;
                    if(userType==0){
                        demo = new Demo(zczjzappkeyIos, zczjzappMasterSecretIos);
                        try {
                            //iOS推送 家长版
                            demo.sendIOSUnicast(pushMsg);
                           // logger.info("ios家长版 其他消息推送成功"+pushMsg.getTokenId());
                        } catch (Exception e) {
                            e.printStackTrace();
                            logger.error("ios 家长版其他消息推送失败"+pushMsg.getTokenId());
                        }
                    } else {
                        demo = new Demo(zczjsappkeyIos, zczjsappMasterSecretIos);
                        try {
                            //iOS推送 教师版
                            demo.sendIOSUnicast(pushMsg);
                           // logger.info("ios 教师版 其他消息推送成功"+pushMsg.getTokenId());
                        } catch (Exception e) {
                            e.printStackTrace();
                            logger.error("ios 教师版 其他消息推送失败"+pushMsg.getTokenId());
                        }
                    }
                }

            }
        }
        System.err.println("其他信息推送"+pushMsgs);
        return pushMsgs;
    }
    /**
     *
     *获取推送的交流消息列表(不能超过495条数据,只推送近10分钟未推送的消息;当推送出现错误，允许重试2次，累计推送3次)
     * 每3秒读取一次
     * @return
     */
    public List<PushMsg> selecExchangeList ()  {
        logger.info("正在推送----交流----消息");
        List<PushMsg> pushMsgs =null;
        Integer iOnePageNum = 495;
        Map<String ,Object>map = new HashMap<>();
        map.put("iOnePageNum",iOnePageNum);
        map.put("v_result",null);

        pushMsgMapper.selectExchangeList(map);

        pushMsgs = (List<PushMsg>) map.get("v_result");
        //pushMsgs = pushMsgMapper.selectExchangeList();
        if (pushMsgs != null){
            System.err.println("交流消息安卓");
            for (PushMsg pushMsg : pushMsgs) {
                Integer deviceType = pushMsg.getDeviceType();
                Integer userType = pushMsg.getUserType();
                if (deviceType==0 || deviceType==2){
                    Demo demo =null;
                    if(userType==0){
                        demo = new Demo(zczjzappkeyAndroid, zczjzappMasterSecretAndroid);
                        try {
                            //安卓推送 家长版
                            demo.sendAndroidUnicast(pushMsg,pushMsg.getUserType());
                        } catch (Exception e) {
                            e.printStackTrace();
                            logger.error("家长 交流消息推送失败"+pushMsg.getTokenId());
                        }
                    } else {
                        demo = new Demo(zczjsappkeyAndroid, zczjsappMasterSecretAndroid);
                        try {
                            //安卓推送 教师版
                            demo.sendAndroidUnicast(pushMsg,pushMsg.getUserType());
                        } catch (Exception e) {
                            e.printStackTrace();
                            logger.error("教师版 交流消息推送失败"+pushMsg.getTokenId());
                        }
                    }
                }else {
                    System.err.println("交流消息iOS");
                    Demo demo =null;
                    if(userType==0){
                        demo = new Demo(zczjzappkeyIos, zczjzappMasterSecretIos);
                        try {
                            //iOS推送家长版
                            demo.sendIOSUnicast(pushMsg);
                        } catch (Exception e) {
                            e.printStackTrace();
                            logger.error("家长iOS交流消息推送失败"+pushMsg.getTokenId());
                        }
                    } else {
                        demo = new Demo(zczjsappkeyIos, zczjsappMasterSecretIos);
                        try {
                            //iOS推送 教师
                            demo.sendIOSUnicast(pushMsg);
                        } catch (Exception e) {
                            e.printStackTrace();
                            logger.error("教师iOS交流消息推送失败"+pushMsg.getTokenId());
                        }
                    }
                }

            }
        }
        System.err.println("交流信息"+pushMsgs);
        return pushMsgs;
    }

    public List<PushMsg> callProcedure(){
        Integer iOnePageNum = 495;
        Map<String ,Object>map = new HashMap<>();
        map.put("iOnePageNum",iOnePageNum);
        map.put("v_result",null);

        pushMsgMapper.callProcedure(map);

        List<PushMsg>v_result = (List<PushMsg>) map.get("v_result");
        System.err.println(v_result);
        return v_result;
    }

}
